// i.d(t, { n: () => MattertagsData })
import Data from "../object/Data"
import ObservableMap, { createMap } from "../observer/observable.map"
import Mattertag from "../object/Mattertag"
export default class MattertagsData extends Data {
  mattertags: ObservableMap<Mattertag>
  constructor(e?: Mattertag[]) {
    super()
    this.name = "tag"
    this.mattertags = createMap()
    if (e) {
      for (const t of e) this.mattertags.set(t.sid, t)
      this.commit()
    }
  }
  clear() {
    for (const e of this.mattertags) this.removeTag(e.sid)
  }
  clone() {
    return new MattertagsData().copy(this)
  }
  copy(e: MattertagsData) {
    MattertagsData.copyTagCollection(this, e)
    return this
  }
  static copyTagCollection(e: MattertagsData, t: MattertagsData) {
    const i = e.mattertags
    const n = t.mattertags
    for (const t of i.keys) n.has(t) || e.removeTag(t)
    for (const t of n) e.addTag(t)
  }
  get collection() {
    return this.mattertags
  }
  addTag(e: Mattertag) {
    this.mattertags.has(e.sid) ? this.mattertags.get(e.sid).copy(e) : this.mattertags.set(e.sid, e.clone())
  }
  removeTag(e: string) {
    return this.mattertags.delete(e)
  }
  getTag(tid: string) {
    return this.mattertags.get(tid)
  }
  getTagList() {
    return this.mattertags.keys
  }
  getTagCount() {
    return this.mattertags.length
  }
  iterate(e: (v: Mattertag) => void) {
    for (const t of this.mattertags) e(t)
  }
  *[Symbol.iterator]() {
    for (const e of this.mattertags) yield e
  }
}
